﻿Public Class frmMain

    Dim Mapa(49, 49) As Label
    Dim sCliente As New TcpClient
    Dim Completado As Boolean = False
    Dim NS As NetworkStream

    Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Keys.Escape
                MsgBox("Escape")
            Case Keys.Space
                Dibujo(5, 5, Colores.Verde)
        End Select
    End Sub

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Barra.Maximum = 50 * 50
        Barra.Value = 0
    End Sub

    Private Sub frmMain_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        MSG.Text = Me.Height & " - " & Me.Width
    End Sub

    Private Sub frmMain_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        Dim F As New frmConect
        F.Show(Me)
        AddHandler F.Conectar, AddressOf ConectarAhora

        Me.Enabled = False
        MSG.Text = "Cargando..."

        For x = 0 To 49
            For y = 0 To 49
                Dim Shape As New Label
                Mapa(x, y) = Shape
                Shape.Location = New System.Drawing.Point(x * 4 + 20, y * 4 + 20)
                Shape.Size = New System.Drawing.Size(5, 5)
                Shape.BorderStyle = BorderStyle.FixedSingle
                'Shape.BackColor = System.Drawing.Color.Red

                Shape.Text = ""
                Controls.Add(Shape)
                Barra.Value += 1
                My.Application.DoEvents()
            Next
        Next

        Barra.Value = 0
        MSG.Text = "Listo"
        Me.Enabled = True
        Completado = True
    End Sub

    Private Sub ConectarAhora(ByVal Server As String, ByRef Conectado As Boolean)
        Conectado = False
        Try
            sCliente.Connect(Server, 2525)
            NS = sCliente.GetStream()
            ActivoLecturaAsincrona()
        Catch ex As Exception
            MsgBox("Error al conectar: " & ex.Message)
            Return
        End Try

        Conectado = True
        Return
    End Sub

    Private Enum Colores As UInteger
        Blanco
        Verde
        Rojo
    End Enum

    Private Sub Dibujo(ByVal x As UInt16, ByVal y As UInt16, ByVal Color As Colores)
        If Not Completado Then Return

        Select Case Color
            Case Colores.Rojo
                Mapa(x, y).BackColor = Drawing.Color.Red
            Case Colores.Verde
                Mapa(x, y).BackColor = Drawing.Color.Green
            Case Colores.Blanco
                Mapa(x, y).BackColor = Drawing.Color.White
            Case Else
                MsgBox("Error de dibujo, color")
        End Select

    End Sub

    Dim Buffer(2) As Byte
    Private Sub ActivoLecturaAsincrona()
        NS.BeginRead(Buffer, 0, Buffer.Length, AddressOf Leer, Nothing)
    End Sub

    Private Sub Leer(ByVal ar As IAsyncResult)
        Dim Largo As Integer = NS.EndRead(ar)
        If Largo > 0 Then
            Dim X As Integer = Buffer(0)
            Dim Y As Integer = Buffer(1)
            Dim Color As Colores = Buffer(2)

            If X >= 0 And X < 50 Then
                If Y >= 0 And Y < 50 Then
                    Dibujo(X, Y, Color)
                End If
            End If
            ActivoLecturaAsincrona()
        End If
    End Sub
End Class
